package com.itheima.dao.impl;

import com.itheima.bean.Employee;
import com.itheima.dao.IEmployeeDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmployeeDaoImpl1 implements IEmployeeDao {

    JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void  findAllEmployee() {
        List<Employee> employees = jdbcTemplate.query("select * from employee",new BeanPropertyRowMapper<Employee>(Employee.class));
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    public Employee findEmployeeById(Integer id) {
        List<Employee> employees = jdbcTemplate.query("select * from employee where emp_id = ?",new BeanPropertyRowMapper<Employee>(Employee.class),id);
        return employees.isEmpty()?null:employees.get(0);
    }

    public void saveEmployee(Employee employee) {
        jdbcTemplate.update("insert into employee(emp_name,salary) values (?,?)",employee.getEmpName(),employee.getSalary());
    }

    public void updateEmployee(Integer id,Double salary) {
        jdbcTemplate.update("update employee set salary = ? where emp_id = ?",salary,id);
    }

    public void deleteEmployee(Integer id) {
        jdbcTemplate.update("delete from employee where emp_id = ?",id);
    }
}
